import React from 'react';
import {injectIntl, language} from '../intl';
import PropTypes from 'prop-types';
import {Button, Form, Row, Col} from 'antd';
import TextInput from 'Shared/components/TextInput';
import {FORM_OPTIONS} from '../../constants';
import DateRangePicker from 'Shared/components/DateRangePicker';
import WrappedSelect from '../../common/WrappedSelect';
import {subClaimStatusHis, claimHisType, originTag, dealerType, isYesNo} from '../../Enum';
import DealerSelectPanel from '../../common/DealerSelect/SelectEdit';
import {hasPermissions} from '../utils';
import {PERMISSION} from '../constants';
import Export from '../../common/Export';
const subClaimStatusHisList = subClaimStatusHis.toList(language);
const claimHisTypeList = claimHisType.toList(language);
const originTagList = originTag.toList(language);
const isYesNoList = isYesNo.toList(language);
const FormItem = Form.Item;
const dealerConditions = {
    type: dealerType.经销商
};
class HistoryQueryPanel extends React.PureComponent {
    state = {
        loading: false
    }
    handleFilter = (value, name) => {
        this.props.onHistoryConditionsChange(name, value);
    };
    handleFilterPressEnter = (value, name) => {
        this.props.onHistoryConditionsChange(name, value);
        this.onClickQuery();
    };

    onClickQuery = () => {
        this.props.querySearch();
    }

    onSelectDealer = data => {
        this.props.onHistoryConditionsChange('subDealerId', data.id);
        this.props.onSelect({
            id: data.id,
            code: data.code,
            name: data.name,
        });
    }
    render() {
        const dealerInfo = this.props.conditions.subDealerId ? {
            id: this.props.queryHistoryDealer.id,
            code: this.props.queryHistoryDealer.code,
            name: this.props.queryHistoryDealer.name
        } : null;
        return (
            <div className="form-standard">
                <Form className="form-standard">
                    <Row>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.code',
                                    defaultMessage: '索赔单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="code"
                                    value={this.props.conditions.code}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.dealerClaimCode',
                                    defaultMessage: '总代索赔单号'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="dealerClaimCode"
                                    value={this.props.conditions.dealerClaimCode}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter}/>
                            </FormItem>
                        </Col>
                        {this.props.isDealerUser &&
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.subDealerId',
                                    defaultMessage: '经销商'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DealerSelectPanel
                                    conditions={dealerConditions}
                                    value={dealerInfo}
                                    onSelect={this.onSelectDealer} />
                            </FormItem>
                        </Col>
                        }
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.status',
                                    defaultMessage: '状态'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="status"
                                    value={this.props.conditions.status}
                                    options={subClaimStatusHisList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.claimType',
                                    defaultMessage: '索赔类型'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="claimType"
                                    value={this.props.conditions.claimType}
                                    options={claimHisTypeList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.vin',
                                    defaultMessage: 'VIN'
                                })}
                                {...FORM_OPTIONS.item}>
                                <TextInput
                                    name="vin"
                                    value={this.props.conditions.vin}
                                    onPressEnter={this.handleFilterPressEnter}
                                    onBlur={this.handleFilter}/>
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.originTag',
                                    defaultMessage: 'CBU/KD'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="originTag"
                                    value={this.props.conditions.originTag}
                                    options={originTagList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.createTime',
                                    defaultMessage: '上报日期'
                                })}
                                {...FORM_OPTIONS.item}>
                                <DateRangePicker
                                    name="createTime"
                                    value={this.props.conditions.createTime}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                        <Col {...FORM_OPTIONS.col}>
                            <FormItem
                                label={this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.form.isDc',
                                    defaultMessage: '是否地产化的'
                                })}
                                {...FORM_OPTIONS.item}>
                                <WrappedSelect
                                    allowClear
                                    name="isDc"
                                    value={this.props.conditions.isDc}
                                    options={isYesNoList}
                                    onChange={this.handleFilter} />
                            </FormItem>
                        </Col>
                    </Row>
                    <Row className="operation-buttons">
                        <Col span={6}>
                            <Button
                                key="query"
                                type="primary"
                                onClick={this.onClickQuery}
                                loading={this.props.loading}>
                                {this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.btn.query',
                                    defaultMessage: '查询'
                                })}
                            </Button>
                            <Button
                                key="clear"
                                onClick={this.props.onClickReset}>
                                {this.props.intl.formatMessage({
                                    id: 'historyQueryPanel.btn.reset',
                                    defaultMessage: '重置'
                                })}
                            </Button>
                        </Col>
                        <Col span={18} className="col-align-right">
                            {hasPermissions(this.props.permissions, PERMISSION.historyExport) &&
                            <Export key={PERMISSION.historyExport} exportButton={this.props.intl.formatMessage({
                                id: 'queryPanel.btn.historyExport',
                                defaultMessage: '导出'
                            })} exportRequest={this.props.onClickExportHistory} />}
                        </Col>
                    </Row>
                </Form>
            </div>
        );
    }
}

HistoryQueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExportHistory: PropTypes.func.isRequired,
    onClickReset: PropTypes.func.isRequired,
    onConditionsPressEnter: PropTypes.func.isRequired,
    onHistoryConditionsChange: PropTypes.func.isRequired,
    onSelect: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {saveHistoryQueryCondition, queryHistorySearch, resetHistoryQueryPanel, selectHistoryDealer, exportHistoryData} from '../actions';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getConditions = selectorFactory(['page', 'appState', 'historyQueryCondition']);
const getQueryHistoryDealer = selectorFactory(['page', 'appState', 'queryHistoryDealer']);
const getDealerUser = selectorFactory(['page', 'domainData', 'initData', 'isDealerUser']);
const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    queryHistoryDealer: getQueryHistoryDealer(state),
    loading: state.getIn(['page', 'domainData', 'historyList', 'isFetching']),
    isDealerUser: getDealerUser(state),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onHistoryConditionsChange: (name, value) => dispatch(saveHistoryQueryCondition(name, value)),
    onConditionsPressEnter: (value, name) => {
        dispatch(saveHistoryQueryCondition(name, value));
        dispatch(queryHistorySearch());
    },
    querySearch: () => dispatch(queryHistorySearch()),
    onClickReset: () => dispatch(resetHistoryQueryPanel()),
    onSelect: data => dispatch(selectHistoryDealer(data)),
    onClickExportHistory: () => dispatch(exportHistoryData()),
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(injectIntl(HistoryQueryPanel));
